<?php
namespace src;
use classlib\input;
use src\logic\Cadmin;
use classlib\core\lang;
use classlib\core as C;
use classlib\core\controller;
use src\logic\workflow\doc_executor;
class Cmonitor extends controller {
	//流程监控 可以监控所有的流程实例
	public function main() {
		$view = C\view::inst('extjs');
		Cadmin::chkaccess($view);
		$dlang= lang::inst('dd');
		$view->assign('estatus', $dlang->get('wk00001'));
		$view->assign('nstatus', $dlang->get('wk00002'));
		$view->assign_by_ref('title', Cadmin::$name);
		$view->display('workflow/monitor.ext.js');
	}

	/**
	 * @desc    流程监控列表资料信息
	 * @doc     流程监控
	 * @access  需要授权
	 */
	public function dolist() {
		$view  = C\view::inst('json');
		Cadmin::chkaccess($view, 'workflow/monitor');
		list($start, $limit, $sort) = Cadmin::offset();
		$where = array();
		$name  = input::post('name');
		$worm  = C\model::load('main.proc.workflow');
		if(!empty($name)) {
			$cond= array(array('name', '%'.$name.'%', \classlib\orm::OP_LIKE));
			$ids = $worm->get_column(0, -1, $cond, 'id');
			if ($ids) {
				$where[]= array('id', $ids, \classlib\orm::OP_IN);
			}
		}
		$pid   = input::post('pid');
		if(is_numeric($pid)) {
			$where[]= array('pid', $pid, \classlib\orm::OP_EQ);
		}
		$id    = input::post('id');
		if(is_numeric($id)) {
			$where[]= array('id', $id, \classlib\orm::OP_EQ);
		}
		$overid= input::post('overid');
		if(is_numeric($overid)) {
			$where[]= array('overid', $overid, \classlib\orm::OP_EQ);
		}
		$workflowid = input::post('workflowid');
		if(is_numeric($workflowid)) {
			$where[]= array('id', $workflowid, \classlib\orm::OP_EQ);
		}
		$status = input::post('status');
		if(is_numeric($status)) {
			$where[]= array('status', $status, \classlib\orm::OP_EQ);
		}
		$orm   = C\model::load('main.proc.execution');
		$total = $orm->get_total($where);
		$list  = array();
		if ($total > 0) {
			$list = $orm->get_list($start, $limit, $where, '*', $sort);
			foreach ($list as &$item) {
				$workflow         = $worm->get_one($item['workflowid']);
				$item['title']    = $workflow['name'];
				$item['stime']    = doc_executor::datetime($item['stime']);
				$item['etime'] 	  = doc_executor::datetime($item['etime']);
				$item['stoptime'] = doc_executor::datetime($item['stoptime']);
				$item['optime']   = doc_executor::datetime($item['optime']);
			}
		}
		$view->assign_by_ref('total', $total);
		$view->assign_by_ref('dataset', $list);
		$view->display();
	}

	//获取节点信息列表
	public function donode() {
		$view  = C\view::inst('json');
		Cadmin::chkaccess($view, 'workflow/monitor');
		$executionid = input::post('executionid');
		$list        = array();
		if ($executionid > 0) {
			$where   = array();
			$where[] = array('executionid', $executionid, \classlib\orm::OP_EQ);
			$norm    = C\model::load('main.proc.node');
			$sorm    = C\model::load('main.proc.nodestate');
			$list    = $sorm->get_list(0, -1, $where, '*', 'id', \classlib\orm::ASC);
			foreach ($list as &$item) {
				$item['accepttime']  = doc_executor::datetime($item['accepttime']);
				$item['completetime']= doc_executor::datetime($item['completetime']);
				$item['createtime']  = doc_executor::datetime($item['createtime']);
				$item['lastmodify']  = doc_executor::datetime($item['lastmodify']);
				$item['expire']      = doc_executor::datetime($item['expire']);
				$node                = $norm->get_one($item['nodeid']);
				$item['name']        = $node['name'];
				$item['preid']       = $node['preid'];
			}
		}
		$view->assign_by_ref('dataset', $list);
		$view->display();
	}

	/**
	 * @desc    流程终止列表资料信息
	 * @doc     终止流程
	 * @access  需要授权
	 */
	public function dostop() {
		$view  = C\view::inst('json');
		$userid= Cadmin::chkaccess($view);
		$lang  = C\lang::inst('adm');
		$id    = (int)input::post('id');
		$remark= input::post('remark');
		if ($id < 1 || empty($remark)) {
			$view->assign_error('50006', $lang->get('workflow', '50006'));
		}
		$eorm = C\model::load('main.proc.execution');
		$item = $eorm->get_one($id);
		if (empty($item)) {
			$view->assign_error('50007', $lang->get('workflow', '50007'));
		}
		//流程已经结束或者终止，则不能再次执行
		if ($item['status'] == doc_executor::ENDSUB || $item['status'] == doc_executor::TERMINATE) {
			$view->assign_error('50008', $lang->get('workflow', '50008'));
		}
		$remarks = $item['remark'].'['.$remark.']';
		$disname = doc_executor::disname($userid);
		$data    = array('stopuserid'=>$userid, 'stopusername'=>$disname, 'remark'=>$remarks,
			 'status'=>doc_executor::TERMINATE, 'stoptime'=>C\config::$nowtime);
		$eorm->save($id, $data);
		//终止该节点的子流程 递归遍历
		$i    = 0; //统计处理的子流程的数量
		$eids = array($id);
		while (true) {
			$where   = array();
			$where[] = array('pid', $eids, \classlib\orm::OP_IN);
			$items   = $eorm->get_list(0, -1, $where);
			if (count($items) < 1) {
				break;
			}
			$eids    = array();
			foreach ($items as $item) {
				$eids[] 		= $item['id'];
				$remarks 		= $item['remark'].'['.$remark.']';
				$data['remark'] = $remarks;
				$eorm->save($item['id'], $data);
				$i++;
			}
		}
		$msg = $lang->get('workflow', '50009');
		$view->assign('msg', sprintf($msg, $i));
		$view->display();
	}
}
